#!/usr/bin/env perl -w

use strict;
use warnings;

use XML::Dumper;
use Env qw(HOME);
use Getopt::Std;
use Pod::Usage;
use Digest::MD5 qw(md5_hex);

my %opts;
getopt('suprld', \%opts);

$opts{'s'}		||= 'localhost';
$opts{'u'}		||= 'anonymous';
$opts{'p'}		||= 'someuser@';
$opts{'r'}		||= '/';
$opts{'l'}		||= '.';
$opts{'d'}		||= 'default';

if ($opts{'h'})
{
    pod2usage({-exitval => 2,
	       -verbose => 0});
}

#XML dumper object
my $d;

#configuration
my $hash;

#a true/false tool
my $b = 0;

#loop tool
my $a = 0;

#THIS SHOULD BE THE SAME AS IN ftp_sync_config.pl
my $configDir = $HOME . '/.perliwebftpuploader';

# Location of the configuration File
my $configFile = $configDir . '/config.xml';

#Create a unique file name
my $data = $opts{'d'} . "_" . md5_hex( $opts{'s'}, $opts{'u'}, $opts{'r'}, $opts{'l'}, $opts{'d'} );

$d = new XML::Dumper;

if ( ! -e $configDir )
{
	mkdir ( $configDir );
}

if ( ! -e $configDir . '/data' )
{
	mkdir ( $configDir . '/data');
}


#Get rid of any trailing slashes on directory
my $dir;
my @dirs;

#remote dir
$dir = $opts{'r'};
@dirs = split( "/", $dir );
if( $#dirs > 0 ) # this takes care of the instance where remote dir is "/"
{
$opts{'r'} = join( "/", @dirs );
}

#local dir
$dir = $opts{'l'};
@dirs = split( "/", $dir );
if( $#dirs > 0 )
{
$opts{'l'} = join( "/", @dirs );
}


if ( -s $configFile )
{
	$hash = $d->xml2perl($configFile);
	
	foreach my $h (@$hash) 
	{
		if ( $$h{'des'} eq $opts{'d'} )
		{
			$b = 1;
			print "Updating configuration $opts{'d'}\n";
			$opts{'s'} =|| 
			
			$$hash[$a] = {		server		=> $opts{'s'},
								user		=> $opts{'u'},
								password	=> $opts{'p'},
								remote_dir	=> $opts{'r'},
								local_dir	=> $opts{'l'},
								des			=> $opts{'d'},
								data		=> $data
					  		} 
		}
		$a = $a + 1;
	}
}


if ( $b != 1 )
{	
	print "Adding configuration $opts{'d'}\n";
	push(@$hash, {	server		=> $opts{'s'},
					user		=> $opts{'u'},
					password	=> $opts{'p'},
					remote_dir	=> $opts{'r'},
					local_dir	=> $opts{'l'},
					des			=> $opts{'d'},
					data		=> $data
					  } );
}

$d->perl2xml($hash, $configFile);


__END__

=pod

=head1 NAME

ftp_sync_config.pl - Sync a hierarchy of local files with a remote FTP repository

=head1 SYNOPSIS

ftp_sync_config.pl [-s server] [-u username] [-p password] [-r remote directory] [-l local directory] [-d description of this configuration]


=head1 DESCRIPTION

This perl code was developed for use specifically with iWeb. 
It's not perfect at the moment but seeing as how apple didn't 
make a very convenient way to upload your iWeb documents onto
a ftp server I figured this would be helpful. 

You could also use the code to synchronize any directory with another 
directory on an ftp server. It assumes that it's being run on
Mac OS X with perl version 5.8.8

=head1 BUGS

The synchronization is not quite complete. This script does not deal
with symbolic links, and files with multibyte charecters.

=head1 AUTHORS

Justin

=head1 SEE ALSO

Perl(1).

=cut
